package com.itheima.web.controller.sms;

import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.sms.SmsHomeAdvertiseService;
import com.itheima.vo.sms.SmsHomeAdvertise;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/home/advertise")
@Slf4j
public class SmsHomeAdvertiseController {

    @DubboReference
    private SmsHomeAdvertiseService smsHomeAdvertiseService;

    /**
     * 添加广告
     */
    @PostMapping("/create")
    public ResultData create(@RequestBody SmsHomeAdvertise smsHomeAdvertise) {
        log.info("添加广告信息:{}",smsHomeAdvertise);
        smsHomeAdvertiseService.create(smsHomeAdvertise);
        return ResultData.ok("操作成功", "");
    }

    /**
     * 删除广告
     */
    @PostMapping("/delete")
    public ResultData delete(@RequestParam("ids") List<Long> ids) {
        boolean res = smsHomeAdvertiseService.removeBatchByIds(ids);
        if (res) {
            return ResultData.ok("删除成功", "");
        } else {
            return ResultData.error("删除失败");
        }
    }



    /**
     * 分页查询广告
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String name, String endTime, Integer pageNum, Integer pageSize, Integer type){


        PageResult pageResult = smsHomeAdvertiseService.selectPage(name, endTime, pageNum, pageSize, type);
        return ResultData.ok("", pageResult);
    }

    /**
     * 修改上下线状态
     */
    @PostMapping("/update/status/{id}")
    public ResultData updateStatus(@PathVariable("id") Long id, @RequestParam("status") Integer status) {
        smsHomeAdvertiseService.updateStatus(id, status);
        return ResultData.ok("修改成功", "");
    }

    /**
     * 修改广告
     */

    @PostMapping("update/{id}")
    public ResultData update(@PathVariable("id") Long id, @RequestBody SmsHomeAdvertise smsHomeAdvertise) {
        boolean res = smsHomeAdvertiseService.updateById(smsHomeAdvertise);
        return ResultData.ok("修改成功", "");
    }

    /**
     * 获取广告详情
     */
    @GetMapping("{id}")
    public ResultData getById(@PathVariable("id") Long id) {
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseService.getById(id);
        return ResultData.ok("", smsHomeAdvertise);
    }

}
